package org.i1510.wx.service.impl;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.i1510.wx.bean.req.ReqBaseMessage;
import org.i1510.wx.bean.req.TextMessage;
import org.i1510.wx.bean.resp.ArticleResp;
import org.i1510.wx.bean.resp.GraphicMessageResp;
import org.i1510.wx.bean.resp.TextMessageResp;
import org.i1510.wx.constant.MessageConstant;
import org.i1510.wx.po.WxPublic;
import org.i1510.wx.service.ISendMessageService;
import org.i1510.wx.utils.ObjectToXmlUtils;

public class RespMessageServiceImpl implements ISendMessageService{
	

	private static Logger logger = Logger.getLogger(RespMessageServiceImpl.class);

	
	@Override
	public String sendTextMessage(String to,String from,String content) {
		TextMessageResp message = new TextMessageResp();
		message.setFromUserName(from);
		message.setToUserName(to);
		message.setMsgType("text");
		Long d = System.currentTimeMillis()/1000;
		message.setCreateTime(d.intValue());
		message.setContent(content);
		return ObjectToXmlUtils.textMessageToXml(message);
	}
	@Override
	public String sendMessage(ReqBaseMessage reqMessage,WxPublic wxPublic) {
		
		if(reqMessage.getMsgType().equals(MessageConstant.REQ_MESSAGE_TYPE_TEXT)){
			//接收文本信息回复,调用规则引擎			
			TextMessage tm = (TextMessage)reqMessage;
			if(tm.getContent().toUpperCase().contains("BOBO")){
				return sendTextMessage(reqMessage,wxPublic);
			}else if(tm.getContent().toUpperCase().contains("@注册@")){
				return sendTextMessage4Reg(reqMessage,wxPublic);
			}else{
				return sendGraphicMessage(reqMessage,wxPublic);
			}
		}else{
			return sendGraphicMessage(reqMessage,wxPublic);
		}
		
		
	}
	private String sendTextMessage4Reg(ReqBaseMessage reqMessage,WxPublic wxPublic){
		TextMessageResp message = new TextMessageResp();
		message.setFromUserName(reqMessage.getToUserName());
		message.setToUserName(reqMessage.getFromUserName());
		message.setMsgType("text");
		Long d = System.currentTimeMillis()/1000;
		message.setCreateTime(d.intValue());
		message.setContent("您提交注册信息成功!");
		return ObjectToXmlUtils.textMessageToXml(message);
	}
	/**
	*@Author Rambo
	*@Desc：发送文本消息（私有方法）
	*@param reqMessage
	*@param wxPublic
	*@return TODO
	*@Version  RespMessageServiceImpl.java,v 1.1 2014-10-17 下午4:54:13 
	*/
	private String sendTextMessage(ReqBaseMessage reqMessage,WxPublic wxPublic){
		TextMessageResp message = new TextMessageResp();
		message.setFromUserName(reqMessage.getToUserName());
		message.setToUserName(reqMessage.getFromUserName());
		message.setMsgType("text");
		Long d = System.currentTimeMillis()/1000;
		message.setCreateTime(d.intValue());
		message.setContent("您发送的是文本信息!");
		return ObjectToXmlUtils.textMessageToXml(message);
	}
	//单图文
	private String sendGraphicMessage(ReqBaseMessage reqMessage,WxPublic wxPublic){
		
		GraphicMessageResp gm = new GraphicMessageResp();
		gm.setArticleCount("1");
		List<ArticleResp> articleList = new ArrayList<ArticleResp>();
		ArticleResp article = new ArticleResp();
		article.setTitle("单图文标题");
		article.setDescription("描述");
		String picUri = "http://www.mftp.info/20141001/1413786963x-637121238.jpg";
		article.setPicUrl(picUri);
		article.setUrl("www.baidu.com");
		articleList.add(article);
		gm.setArticles(articleList);
		
		
		gm.setFromUserName(reqMessage.getToUserName());
		gm.setToUserName(reqMessage.getFromUserName());
		gm.setMsgType("news");
		Long d = System.currentTimeMillis()/1000;
		gm.setCreateTime(d.intValue());
		return ObjectToXmlUtils.graphicMessage(gm);
	}



	
}
